import api from '../../utils/request/api'
import { goods } from '../../utils/request/apiurl'

Page({
  data: {
    imgUrls: [
      'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
      'https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640',
      'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640'
    ],
    currentNav: 0,
    navList: [
      { id: 1, name: '全部', current: true },
      { id: 2, name: '酸牛奶', current: false },
      { id: 3, name: '纯牛奶', current: false },
      { id: 4, name: '风味酸牛奶', current: false }
    ],
    isClassOpen: false,
    currentClassName: '综合分类',
    radioItems: [
      { id: 1, name: '综合分类', checked: true },
      { id: 2, name: '鲜牛奶' },
      { id: 3, name: '风味牛奶' },
      { id: 4, name: '纯牛奶' }
    ],
    listScreen: {
      classId: 1
    },
    salesVolumeIndex: 0,
    priceIndex: 0,
    isRequest: false,
    searchGood: {
      page: 1,
      pages: 0
    },
    goodList: [
      {
        id: 1,
        img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        title: '1蒙牛  纯甄 常温酸牛奶 青柠抹茶牛油果口味200g*16礼盒装',
        oldPrice: 68.00,
        newPrice: 36.60,
        sales: 32515
      },
      {
        id: 2,
        img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        title: '2蒙牛  纯甄 常温酸牛奶 青柠抹茶牛油果口味200g*16礼盒装',
        oldPrice: 68.00,
        newPrice: 36.60,
        sales: 32515
      },
      {
        id: 3,
        img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        title: '3蒙牛  纯甄 常温酸牛奶 青柠抹茶牛油果口味200g*16礼盒装',
        oldPrice: 68.00,
        newPrice: 36.60,
        sales: 32515
      }
    ],
    isPopUp: false,
    couponList: [
      {
        id: 1,
        money: 20,
        fullMoney: 299,
        name: '全品类通用券',
        type: '全平台',
        validity: '2017-08-01'
      },
      {
        id: 2,
        money: 20,
        fullMoney: 299,
        name: '全品类通用券',
        type: '全平台',
        validity: '2017-08-01'
      }
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideTabBar();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
      this.getGoodList(this.data.searchGood.page + 1)
    // }
  },
  /**
   * 事件 Methods
   */
  // 跳转特价商品
  goGoodSpecial () {
    wx.navigateTo({ url: '../good-search/good-search' })
  },
  // 打开分类列表
  openClass () {
    wx.hideTabBar();
    const current = this.data.isClassOpen;
    this.setData({
      isClassOpen: !current
    })
    if (!this.data.isClassOpen) {
      wx.showTabBar();
    }
  },
  // 关闭分类列表
  closeClass () {
    this.setData({
      isClassOpen: false
    })
    wx.showTabBar();
  },
  // 选择分类
  changeTab (event) {
    const currentIndex = event.currentTarget.dataset.index;
    const currentId = event.currentTarget.dataset.id;
    // 给当前项增加class
    if (this.data.currentNav !== currentIndex) {
      this.setData({
        currentNav: currentIndex
      })
    }
  },
  radioChange: function (e) {
    let radioItems = this.data.radioItems;
    for (let i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].id == e.detail.value;
    }
    this.setData({
      radioItems: radioItems
    })
    
    const currentClass = this.data.radioItems.find(item => item.checked);
    this.setData({
      currentClassName: currentClass.name,
      listScreen: {
        classId: currentClass.id
      }
    })
  },
  // 切换销量
  salesVolumeChange (e) {
    let salesVolumeIndex = this.data.salesVolumeIndex;
    if (salesVolumeIndex < 2) {
      this.setData({
        salesVolumeIndex: ++salesVolumeIndex
      })
    } else {
      this.setData({
        salesVolumeIndex: 0
      })
    }
    // console.log(this.data.salesVolumeIndex)
  },
  // 切换价格
  priceChange (e) {
    let priceIndex = this.data.priceIndex;
    if (priceIndex < 2) {
      this.setData({
        priceIndex: ++priceIndex
      })
    } else {
      this.setData({
        priceIndex: 0
      })
    }
    // console.log(this.data.priceIndex)
  },
  // 跳转搜索
  goSearch () {
    wx.navigateTo({ url: '../good-search/good-search' })
  },
  // 获取商品列表
  getGoodList (pageNo) {
    this.setData({
      isRequest: true
    })
    
    // api.get(goods.list).then(res => {
    //   this.setData({
    //     goodList: res
    //   })
    // }).catch(err => {
    //   wx.showToast({
    //     title: err.message,
    //     icon: 'none'
    //   })
    // })

    this.setData({
      goodList: this.data.goodList.concat([
        {
          id: 4,
          img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
          title: '4蒙牛  纯甄 常温酸牛奶 青柠抹茶牛油果口味200g*16礼盒装',
          oldPrice: 68.00,
          newPrice: 36.60,
          sales: 32515
        }
      ])
    })

    this.setData({
      isRequest: true
    })

  },
  // 领取优惠券
  handleReceive () {
    console.log('领取成功')
    this.setData({
      isPopUp: true
    })
    wx.showTabBar()
  },
  // 关闭弹窗
  closePopup () {
    this.setData({
      isPopUp: true
    })
    wx.showTabBar()
  }
})